
// 充值弹窗
// import vipModal from "@/socialMedia/Vip/vipModal.vue";
// 获取配置文件, 判断对应路由需要加载的文件模块

if (!localStorage.getItem("USER_ACCESS_SOURCE_INFO")) {
  localStorage.setItem(
    "USER_ACCESS_SOURCE_INFO",
    JSON.stringify({
      user_agent: navigator.userAgent,
      source_url: document.referrer,
    })
  );
}

import { createApp } from "vue";
import App from "./App.vue";
import "ant-design-vue/dist/antd.less";
import appUse from "./common/utils/app_use";
import "./common/styles/common.less";
import "./common/utils/permission";
import "./common/styles/global.css";
import LoginModal from "./components/LoginModal/index.vue";
import hljs from "highlight.js";
import { VueMasonryPlugin } from "vue-masonry";
import falls from "vue-falls";
import ShotstackLoginModal from "./components/ShotstackLoginModal/index.vue";
import store from "./store";
import routes from "./router/router.config.js";
import utils from "@/common/utils/utils.js";

// 自动获取焦点指令
import { autoFocus } from "@/views/shotstack/MuseMixedCut/utools/index.js";
import axios from "axios";
//懒加载
import VueLazyload from "vue-lazyload";
import { Boot } from "@wangeditor/editor";
import AIModule from "./components/SceneEditor/ai-plugins/module";
import RouteTitle from "./components/RouteTitle/index.vue";
import microApp from "@micro-zoe/micro-app";
import request from "@/common/utils/request.js";
import { computed } from "vue";
import {clearToken} from '@/common/utils/storage.js'
// 时区相关
import useTimezone from '@/hooks/timezone';


import defaultSetting from "./common/utils/defaultSetting";


// canvas 库
import VueKonva from 'vue-konva';

//AI模块
Boot.registerModule(AIModule);
//文章模块

const work_ai_config = computed(() => {
  // TODO 测试结束需注释
  return store?.state?.shotstack?.work_ai_config || {};
});

let app;

function render(props) {
  app = createApp(App);
  app.config.warnHandler = () => null;
  app.component("LoginModal", LoginModal);
  app.component("RouteTitle", RouteTitle);
  app.component("ShotstackLoginModal", ShotstackLoginModal);

  // 全局挂载时区相关
  app.provide('timezone', useTimezone());

  app.directive("highlight", (el) => {
    let blocks = el.querySelectorAll("pre code");
    if (blocks) {
      blocks.forEach((block) => {
        // 创建ol标签元素
        let ol = document.createElement("ol");
        block.parentNode.appendChild(ol);

        hljs.highlightBlock(block);
      });
    }
  });

  app.directive("instruct", {
    mounted(el, { value }) {
      var aTags = el.querySelectorAll("a[chref]");

      aTags.forEach((aTag) => {
        const href = aTag.getAttribute("chref");
        const regex = /([^:]+):\/\/(.+)?/;
        const matches = href.match(regex);

        if (matches && value[matches[1]]) {
          aTag.handleClick = (event) => {
            event.preventDefault();
            value[matches[1]](matches[2]);
          };

          aTag.addEventListener("click", aTag.handleClick);
        }
      });
    },
  });

  // 注册全局组件
  // app.component("vipModal", vipModal);

  // 判断是否是阿里云, 不是阿里云, 将所有的一键成片路由入口关闭
  app.directive("permissionOneClickClip", {
    beforeMount(el, binding) {
      console.log(work_ai_config.value?.shotstack_driver, "是否是阿里云");
      if (work_ai_config.value?.shotstack_driver != "alionline") {
        el.style.display = "none"; // 没有权限则隐藏元素
      }
    },
  });

  app.directive("autoFocus", autoFocus);

  // or with options
  app.use(VueLazyload, {
    attempt: 2,
  });
  microApp.start();
  // microApp.start({
  //   fetch: (url, options) => {
  //     options.credentials = 'include';  // 携带cookie
  //     options.mode = 'cors';            // 强制CORS模式
  //     return window.fetch(url, options);
  //   }
  // });
  microApp.preFetch([
    {
      name: "comment-message",
      url: defaultSetting.replyUrl + '/reply/',
      iframe: true,
    },
  ]);
  appUse(app);
  app.use(VueMasonryPlugin);
  app.use(falls);
  // 挂载 canvas 库
  app.use(VueKonva);

  app.config.globalProperties.$inIframe =
    window.__MICRO_APP_ENVIRONMENT__ || false;

  app.mount(document.getElementById("app"));
}

window.mount = async () => {
  // debugger
  // 获取当前URL参数
  const urlParams = new URLSearchParams(window.location.search);

  // // 删除存储的token
    const token = urlParams.get("token");
    if (token) {
      clearToken()
      store.dispatch('SetUserInfo', null)
      // utils.clearUserToken()
      // setConfige()
    }
    render();
};

window.unmount = () => {
  microApp.unmountAllApps();
  app.unmount();
  app = null;
};

if (!window.__MICRO_APP_ENVIRONMENT__) {
  window.mount();
}
